package com.ssyc.gsk_parents.activity;

import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.OnloadingMoreListener;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.KeyBoardUtils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.adapter.QuanZiRvAdapter;
import com.ssyc.gsk_parents.bean.QuanZiInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的圈子
 * Created by Administrator on 2018/6/25 0025.
 */

public class ParentMyQuanziActivity extends BaseActivity implements View.OnClickListener, View.OnLayoutChangeListener, SrfManager.OnPullRefreshListener, TextWatcher, OnloadingMoreListener {

    //加载框布局
    private RelativeLayout rlLoadingView;

    //返回
    private RelativeLayout rlBack;

    //编辑
    private RelativeLayout rlEdit;

    //列表控件
    private RecyclerView rv;

    //下拉刷新控件
    private SmartRefreshLayout srl;

    private LinearLayout llContent;

    private EditText etInput;

    private Button btSend;

    //Activity最外层的Layout视图
    private View activityRootView;

    //屏幕高度
    private int screenHeight = 0;

    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;

    //适配器
    private QuanZiRvAdapter adapter;

    //数据集合
    private List<QuanZiInfo> oldDatas;

    //头布局
    private View headView;
    private ImageView lvTop, lvHead;
    private TextView tvName;

    //空视图
    private View emptyView;


    //获取评论的当前条目的位置
    private int commentPos;


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_myquanzi;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        //初始化视图
        initView();

        //初始化下拉刷新
        initSrf();

        //设置列表
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }

    /**
     * 初始化视图
     */
    private void initView() {
        rlLoadingView = findView(R.id.rl_loading);
        rlLoadingView.setVisibility(View.VISIBLE);
        rv = findViewById(R.id.rv);
        srl = findViewById(R.id.srl);
        etInput = findViewById(R.id.et_input);
        btSend = findViewById(R.id.bt_send);
        btSend.setOnClickListener(this);
        btSend.setOnClickListener(this);
        llContent = findViewById(R.id.ll_content);
        llContent.setOnClickListener(this);
        activityRootView = findViewById(R.id.rl_main);
        headView = View.inflate(this, R.layout.parent_rv_head_myquanzi, null);
        lvTop = headView.findViewById(R.id.lv_top);
        lvHead = headView.findViewById(R.id.lv_head);
        tvName = headView.findViewById(R.id.tv_name);
        rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(this);
        rlEdit = findViewById(R.id.rl_edit);
        rlEdit.setOnClickListener(this);
    }


    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        //配置下拉刷新控件
        SrfManager.setSmartByPullListener(srl, this);

        //添加输入框的监听事件
        etInput.addTextChangedListener(this);

        //获取屏幕高度
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();

        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight / 3;
    }


    /**
     * 初始化列表控件
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new QuanZiRvAdapter(rv, this, this, R.layout.parent_item_rv_myquanzi, oldDatas);
        adapter.addHeaderView(headView);
    }


    /**
     * 请求服务器数据
     *
     * @param method
     */
    private void http(int method) {

        if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
            rlLoadingView.setVisibility(View.GONE);
        }

        List<QuanZiInfo> newDatas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            QuanZiInfo info = new QuanZiInfo();
            newDatas.add(info);
        }

        if (method == BqaManager.NORML) {
            oldDatas.clear();
            oldDatas.addAll(newDatas);
            BqaManager.setRv(emptyView, this, adapter, rv, this);
        } else {
            //更新数据
            BqaManager.updateRvBySmf(method, srl, rv, oldDatas, newDatas, adapter);
        }
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_back) {
            //返回
            finish();
        } else if (id == R.id.rl_edit) {
            //发布圈子
            UiUtils.startActivity(this, ParentSendCircleActivity.class);
        } else if (id == R.id.bt_send) {
            //发送评论的按钮
            sendCommentAction();
        }
    }

    /**
     * 发送评论内容的方法
     */
    private void sendCommentAction() {
        //获取输入内容
        String inputText = etInput.getText().toString().trim();
        KeyBoardUtils.closeKeyboard(etInput);
        etInput.setText("");

        //隐藏评论布局
        // TODO: 2018/6/25 0025 请求服务器返送评论的内容,成功后更新当前条目的评论的内容
        llContent.setVisibility(View.GONE);
    }


    /**
     * 加入对根据局的管理监听,主要是解决软键盘弹出和关闭的监听状态
     */
    @Override
    protected void onResume() {
        super.onResume();

        //添加layout大小发生改变监听器
        activityRootView.addOnLayoutChangeListener(this);
    }

    /**
     * 事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        if (null != busInfo && "showcontent".equals(busInfo.msg)) {
            //收到显示底部评论布局的事件和弹出软键盘的消息了
            llContent.setVisibility(View.VISIBLE);
            etInput.requestFocus();
            commentPos = busInfo.pos;
            rv.smoothScrollToPosition(busInfo.pos + 1);
            KeyBoardUtils.openKeybord(etInput);
        }
    }

    /**
     * 监听软键盘
     */
    @Override
    public void onLayoutChange(View v, int left, int top, int right,
                               int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
        if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
            if (llContent != null && llContent.getVisibility() == View.GONE) {
                llContent.setVisibility(View.VISIBLE);
            }

        } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
            if (llContent != null && llContent.getVisibility() == View.VISIBLE) {
                llContent.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 输入评论内容的EditText的内容改变监听
     *
     * @param charSequence
     * @param i
     * @param i1
     * @param i2
     */
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        //获取输入内容
        String inputText = etInput.getText().toString().trim();
        if (TextUtils.isEmpty(inputText)) {
            btSend.setBackgroundResource(R.drawable.parent_bt_no_send);
            btSend.setTextColor(getResources().getColor(R.color.unsend));
            btSend.setEnabled(false);
        } else {
            btSend.setBackgroundResource(R.drawable.parent_bt_send);
            btSend.setTextColor(getResources().getColor(R.color.white));
            btSend.setEnabled(true);
        }
    }

}
